package com.ajmd.onlineexam.controller;

import com.ajmd.onlineexam.baseUtil.CommonResult;
import com.ajmd.onlineexam.config.MultiRequestBody;
import com.ajmd.onlineexam.entity.*;
import com.ajmd.onlineexam.enumeration.AppExceptionCodeEnum;
import com.ajmd.onlineexam.enumeration.StaticFinalValue;
import com.ajmd.onlineexam.service.QuestionBankService;
import com.ajmd.onlineexam.service.QuestionService;
import com.ajmd.onlineexam.vo.QuestionVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * @author ajmd
 * @since 2023/4/17 15:40
 */
@RestController
@RequestMapping("/question")
public class QuestionController {
    @Resource
    private QuestionService questionService;
    @Resource
    private QuestionBankService questionBankService;


    @PostMapping("/addOrUpdate")
    public CommonResult addQuestion(@MultiRequestBody QuestionVO questionVO){
        int num = questionService.addOrUpdateQuestion(questionVO);
        if(num==0){
            if(ObjectUtils.isEmpty(questionVO.getId())){
                return CommonResult.error(AppExceptionCodeEnum.INSERT_QUESTION_ERR);
            }else{
                return CommonResult.error(AppExceptionCodeEnum.UPDATE_QUESTION_ERR);
            }

        }
        if(ObjectUtils.isNotEmpty(questionVO.getId())){
            return CommonResult.ok().message("试题编辑成功！");
        }
        //通知题库更新试题数
        questionBankService.updateQuestionTypeNum(questionVO.getBelongQuestionBankId());
        return CommonResult.ok().message("新建试题成功！");

    }

    @PostMapping("/update")
    public CommonResult updateQuestionsDifficulty(@MultiRequestBody List<String> questionIds,
                                       @MultiRequestBody Integer difficulty){

        int num = questionService.batchUpdateDifficulty(questionIds,difficulty);
        if(num==0){
            return CommonResult.error(AppExceptionCodeEnum.UPDATE_QUESTION_DIFFICULTY_ERR);
        }
        return CommonResult.ok().message("试题难度更新成功！");
    }

    @PostMapping("/select")
    public CommonResult getQuestions(@MultiRequestBody String belongQuestionBankId,@MultiRequestBody String account,
                                     @MultiRequestBody Integer userType,@MultiRequestBody Integer currentPage,
                                     @MultiRequestBody Integer pageSize, @MultiRequestBody SearchParam searchParam){

        if(ObjectUtils.isEmpty(belongQuestionBankId) || ObjectUtils.isEmpty(account) || ObjectUtils.isEmpty(userType)){
            return CommonResult.error(AppExceptionCodeEnum.PARAM_ERROR);
        }
        if(StaticFinalValue.STUDENT.equals(userType)){
            return CommonResult.error(AppExceptionCodeEnum.NOT_AUTHORITY);
        }
        QuestionBankPO belongQuestionBankPO = questionBankService.selectById(belongQuestionBankId);
        if(ObjectUtils.isEmpty(belongQuestionBankPO)){
            return CommonResult.error(AppExceptionCodeEnum.QUESTION_BANK_NOT_EXISTS);
        }

        Page<QuestionPO> pageData = questionService.getQuestions(belongQuestionBankId, account, userType, currentPage, pageSize, searchParam);
        return CommonResult.ok().data("pageData",pageData);
    }


    @PostMapping("/selectOne")
    public CommonResult getQuestion(@RequestParam String questionId){
        QuestionVO questionVO = questionService.getQuestion(questionId);
        return CommonResult.ok().data("question",questionVO);
    }

    @PostMapping("/selectPart")
    public CommonResult getQuestionByIds(@MultiRequestBody List<String> questionIds){
        List<QuestionVO> questionVOList = questionService.getQuestionByIds(questionIds);
        if(ObjectUtils.isEmpty(questionVOList)){
            return CommonResult.error(AppExceptionCodeEnum.QUESTION_NOT_EXISTS);
        }
        return CommonResult.ok().data("questionVOList",questionVOList);
    }

    @PostMapping("/delete")
    public CommonResult deleteQuestions(@MultiRequestBody List<String> questionIds,
                                        @MultiRequestBody String questionBankId){
        int num = questionService.deleteBankQuestions(questionIds);
        if(num==0){
            return CommonResult.error(AppExceptionCodeEnum.DELETE_QUESTION_ERR);
        }
        //通知题库更新试题数
        questionBankService.updateQuestionTypeNum(questionBankId);
        return CommonResult.ok().message("试题删除成功！"+num+"条记录被删除！");
    }
}
